﻿package milkmidi.utils
{	
	import flash.display.*;
	import flash.geom.*;	
	public class BitmapUtil extends Object
	{			
		//  ____  ____  _    _  ____ _____ ____  _   _  ____ _____  ____  ____
		// / ___|/ _  \| \  | |/ ___|_   _|  _ \| | | |/ ___|_   _|/ _  \|  _ \
		//| |    || | ||  \ | |\___ \ | | | |_) | | | | |     | |  || | || |_)I
		//| |___ ||_| || | \| | ___) || | | _  /| |_| | |___  | |  ||_| || _  /
		// \____|\____/|_|\___||____/ |_| |_|\_\ \___/ \____| |_|  \____/|_|\_\
		//__________________________________________________________________________________ Constructor
		public function BitmapUtil() { };
		public static function reflect(p_target:*, isAttach:Boolean = false, _numStartFade:Number = 0.4, _numMidLoc:Number = 0.2, _numEndFade:Number = 0, _numSkewX:Number = 0, _numScale:Number = 1):BitmapData
		{
			
			if ((p_target as BitmapData) == null && (p_target as DisplayObject) == null ) 
			{
				throw new Error("error Type");
			}
			var _bmp:BitmapData = new BitmapData(p_target.width, p_target.height, true, 0);				
            var matSkew:Matrix = new Matrix(1, 0, _numSkewX, -1 * _numScale, 0, p_target.height);			
            var recDraw:Rectangle = new Rectangle(0, 0, p_target.width, p_target.height * (2 - _numScale));
            var potSkew:Point = matSkew.transformPoint(new Point(0, p_target.height));
            matSkew.tx = potSkew.x * -1;
            matSkew.ty = (potSkew.y - p_target.height) * -1;
            _bmp.draw(p_target, matSkew, null, null, recDraw, true);
            
            var _gradientShape:Shape = new Shape();
			
			var fillType	:String = GradientType.LINEAR;
		 	var colors		:Array = [0, 0 , 0];
			var alphas		:Array = [_numStartFade, (_numStartFade - _numEndFade) / 2, _numEndFade];
            var ratios		:Array = [0, 0xFF * _numMidLoc, 0xFF];
			var mat			:Matrix = new Matrix();
			var spreadMethod:String = SpreadMethod.PAD;
			
			
            mat.createGradientBox(p_target.width, p_target.height/2, 0.5 * Math.PI);
            _gradientShape.graphics.beginGradientFill(fillType, colors, alphas, ratios, mat, spreadMethod)
            _gradientShape.graphics.drawRect(0, 0, p_target.width, p_target.height);
            _gradientShape.graphics.endFill();
            _bmp.draw(_gradientShape, null, null, BlendMode.ALPHA);
			if(isAttach)
			{
				var bitmap:Bitmap = new Bitmap(_bmp);
				bitmap.y = p_target.height;
				DisplayObjectContainer(p_target).addChild(bitmap);
			}
			_gradientShape = null;
			return _bmp		
		}

		public static function join(p_bmp1:BitmapData, p_bmp2:BitmapData, p_type:String = "down"):BitmapData
		{
			var _spr:Sprite = new Sprite();
			var _bmp1:BitmapData = p_bmp1;
			var _bmp2:BitmapData = p_bmp2;		
			var _bit1:Bitmap = new Bitmap(_bmp1);
			var _bit2:Bitmap = new Bitmap(_bmp2);
			_bit2.y = _bit1.height;
			_spr.addChild(_bit1);
			_spr.addChild(_bit2);
			var _bmp:BitmapData = new BitmapData(_bmp1.width, _bmp1.height + _bmp2.height, true, 0);
			_bmp.draw(_spr);
			
			_bmp1.dispose();
			_bmp2.dispose();
			_bit1 = null;
			_bit2 = null;
			_spr = null;
			
			return _bmp;
			
		}
		public static function mergeBitmapAlpha(p_rect:BitmapData, p_point:BitmapData, p_copyPixels:Boolean = false) : BitmapData
        {
			var _bmp:BitmapData = new BitmapData(p_rect.width, p_rect.height);
            _bmp.copyPixels(p_rect, p_rect.rect, new Point());
            _bmp.copyChannel(p_point, p_point.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
            if (p_copyPixels)
            {
                p_rect.dispose();
                p_point.dispose();
            }
            return _bmp;
        }
		//  ____  ____  _____     ___  _____ _____
		// |  _ \|  _ \|_ _\ \   / / \|_   _| ____|
		// | |_) | |_) || | \ \ / / _ \ | | |  _|
		// |  __/|  _ < | |  \ V / ___ \| | | |___
		// |_|   |_| \_\___|  \_/_/   \_\_| |_____|
        //__________________________________________________________________________________ Private Function
		
		// _______     _______ _    _ _____
		//| ____\ \   / | ____| \  | |_   _|
		//|  _|  \ \ / /|  _| |  \ | | | |
		//| |___  \ V / | |___| | \| | | |
		//|_____|  \_/  |_____|_|\___| |_|
		//__________________________________________________________________________________ Event
		
		//  ____   _____ _____         ____  _____ _____
		// / __ \ | ____|_   _|       / ___|| ____|_   _|
		//||____  |  _|   | |     _   \___ \|  _|   | |
		//||_   _|| |___  | |    (_)   ___) | |___  | |
		// \_|_|  |_____| |_|         |____/|_____| |_|
		//__________________________________________________________________________________ Get Set
		
	}
}
